package com.garygyp.ucenter.controller;


import com.garygyp.commonutils.JwtUtils;
import com.garygyp.commonutils.R;
import com.garygyp.commonutils.ordervo.UcenterMemberOrder;
import com.garygyp.ucenter.entity.Member;
import com.garygyp.ucenter.entity.RegisterVo;
import com.garygyp.ucenter.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author GaryGyp
 * @since 2022-05-17
 */
@CrossOrigin
@RestController
@RequestMapping("/educenter/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    // 1. 登录
    @PostMapping("login")
    public R loginUser(@RequestBody Member member) {
        // member对象包含前端传来的手机号和密码
        // 调用service方法实现登录
        // 返回token值，使用jwt生成
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }

    // 2. 注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    // 3. 根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        // 调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        // 查询数据库，根据用户id获取数据信息
        Member member = memberService.getById(memberId);
        return R.ok().data("userInfo", member);
    }

    // 4. 根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        Member member = memberService.getById(id);
        // 把member对象里面的值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member, ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    // 5. 查询某一天的注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister", count);
    }

}

